<?php
include_once 'lib/config.php';
include_once 'lib/db.php';

function inject_head(){?>
<script>
	var app = {
		url:'proc/admin/rewarddanpunishment.php'};
	
	/**
	 * update table rewarddanpunishment
	 */
	var orderby_old = null;
	var orderby_type = ['ASC', 'DESC'];
	var orderby_count = 0;
	function updateList(key, orderby){
		if (orderby==orderby_old) orderby_count++;	
		else orderby_count=0;
		if ($chk(orderby)){
			orderby_str = orderby+" "+orderby_type[orderby_count%2];
			orderby_old = orderby;
		}
		else{
			orderby_str = "";
			orderby_old = "";
		}
		
		key = key==null? "" : key;
		doRequest(app.url, 'post', 'proc=rnp-table&key='+key+'&orderby='+orderby_str, 
			function (res){ 
//				alert(res);
				$('rnp-table').innerHTML = res;
		});
	}

	//insert a record to table
	function doAdd() {
		FBModal_show2(app.url, 'post', "proc=add-modal", true, true);
	}

	function doSave(form){
		FBModal_loading("Save", "Please wait...", true, false);
		$(form).set('send', {
			onSuccess: function(response) { 
//				alert(response);
				var js = JSON.decode(response);
				var msg = js.error? js.msg : "Process simpan selesai !!!";
				var title = js.error? 'Error' : 'Saving';
				FBModal_show(
					"<h2 class=\"dialog_title\"><span>"+title+"</span></h2>" + 
					"<div class=\"dialog_content\" style=\"padding: 10px 20px\">"+msg+"</div>",
					true, true);

				updateList();
			}
		}).send();
	}

	function doEdit(id){
		FBModal_show2( app.url, 'post', "proc=edit-modal&rnp_id="+id, 
				true, true);
	}

	function doDelete(id){
		FBModal_loading("Delete", "Please wait...", false, false);
		doRequest(app.url, 'post', 'proc=rnp-delete&rnp_id='+id, 
			null,
			function(res){
				//update tablenya
				updateList();
				
				var js = JSON.decode(res);
				var msg = js.error? js.msg : "Process penghapusan selesai !!!";
				var title = js.error? 'Error' : 'Delete';
				
				FBModal_show(
					"<h2 class=\"dialog_title\"><span>"+title+"</span></h2>" + 
					"<div class=\"dialog_content\" style=\"padding: 10px 20px\">"+msg+"</div>",
					true, true);
			});
	}

	window.addEvent('domready', function(){
		updateList();
	});
</script><?php
}

include 'view/header.php';
include 'view/admin/rewarddanpunishment/rnp.php';
include 'view/footer.php';